package top.honlnk.czuan.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.honlnk.czuan.common.constant.MorphemeTypeConstant;
import top.honlnk.czuan.common.constant.RelationTypeConstant;
import top.honlnk.czuan.common.exception.CustomException;
import top.honlnk.czuan.common.result.Result;
import top.honlnk.czuan.pojo.dto.add.AddMorphemeDTO;
import top.honlnk.czuan.pojo.dto.add.AddRelationDTO;
import top.honlnk.czuan.pojo.dto.add.AddTranslationDTO;
import top.honlnk.czuan.pojo.dto.add.AddWordDTO;
import top.honlnk.czuan.service.AddService;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/add")
public class AddController {

    @Autowired
    private AddService addService;

    @PutMapping("/word")
    public Result<String> addWord(@RequestBody AddWordDTO addWordDTO) {
        if (addWordDTO == null) {
            throw new CustomException("参数不能为空");
        }
//        要求单词、音标以及翻译不能为空
        if (addWordDTO.getWord().isEmpty()) {
            throw new CustomException("单词不能为空");
        }
        if (addWordDTO.getTranslationIdList().isEmpty()) {
            throw new CustomException("翻译id不能为空");
        }
        addService.addWord(addWordDTO);
        return Result.success();
    }

    @PutMapping("/translation")
    public Result<Integer> addTranslation(@RequestBody AddTranslationDTO addTranslationDTO) {
        if (addTranslationDTO == null) {
            throw new CustomException("参数不能为空");
        }
        if (addTranslationDTO.getTranslation().isEmpty()) {
            throw new CustomException("翻译不能为空");
        }
        if (addTranslationDTO.getPartSpeechId() == null) {
            throw new CustomException("词性id不能为空");
        }
        Integer translationId = addService.addTranslation(addTranslationDTO);
        return Result.success(translationId);
    }
    @PutMapping("/translation/all")
    public Result<Map<String, Map<Integer, String>>> addTranslationAll(@RequestBody List<AddTranslationDTO> addTranslationDTOList) {
        if (addTranslationDTOList == null) {
            throw new CustomException("参数不能为空");
        }
        Map<String, Map<Integer, String>> translationIdList = addService.addTranslationAll(addTranslationDTOList);
        return Result.success(translationIdList);
    }

    /**
     * 新增词素：词根，前缀，后缀 distance
     * @return String
     */
    @PutMapping("/morphemes")
    public Result<String> addMorphemes(@RequestParam String type, @RequestBody AddMorphemeDTO addMorphemeDTO) {
        if (!MorphemeTypeConstant.ALL_TYPE.contains(type)) {
            throw new CustomException("词素参数不合法");
        }
        if (addMorphemeDTO == null) {
            throw new CustomException("参数不能为空");
        }
        if (addMorphemeDTO.getMorpheme().isEmpty()) {
            throw new CustomException("词素不能为空");
        }
        if (addMorphemeDTO.getTranslationIdList().isEmpty()) {
            throw new CustomException("翻译不能为空");
        }
        addService.addMorpheme(type,addMorphemeDTO);
        return Result.success();
    }

    @PutMapping("/relation")
    public Result<String> addRelation(@RequestParam String type, @RequestBody AddRelationDTO addRelationDTO) {
        if (!RelationTypeConstant.ALL_TYPE.contains(type)) {
            throw new CustomException("关系参数不合法");
        }
        if (addRelationDTO == null) {
            throw new CustomException("参数不能为空");
        }
        if (addRelationDTO.getFormId() == null) {
            throw new CustomException("fromId不能为空");
        }
        if (addRelationDTO.getToId() == null) {
            throw new CustomException("toId不能为空");
        }
        addService.addRelation(type, addRelationDTO);
        return Result.success();
    }
}
